package co.fitstart.admin.entity.search;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.site.Site;


public class AllOrderSearch implements QueryBuilder{

    private String name;
    
    private String orderCode;

    private String mobile;
    
    private String paymentStatus;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeStart;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeEnd;
    
    private List<OrderType> orderTypes;
    
    private Site site;
    
    private List<Site> sites;
    
    private Order order;

    private Filter filter;
    
    /**
     * default constructor
     */
    public AllOrderSearch() {
        order = new Order("createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        PaymentStatus thisPaymentStatus = PaymentStatus.aliasOf(paymentStatus);
        
        if(filter == null || refresh) {
            
            filter =  new Filter().addEquals("disabled", false)
                                  .addEquals("paymentStatus", thisPaymentStatus)
                                  .addSentence("orderType in( :type )", "type", orderTypes)
                                  .addEquals("site", site)
                                  .addSentence("site IN (:sites)", "sites", sites)
                                  .addLike("code", orderCode)
                                  .addLike("user.name", name)
                                  .addLike("user.mobile", mobile)
                                  .addDateTimeBetween("createTime", createTimeStart, createTimeEnd);
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM Order " + getPreparedFilter(false).getWhereSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }
    /**
     * @return the name
     */
    public String getName() {
        return this.name;
    }

    /**
     * @return the orderCode
     */
    public String getOrderCode() {
        return this.orderCode;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return this.mobile;
    }

    /**
     * @return the paymentStatus
     */
    public String getPaymentStatus() {
        return this.paymentStatus;
    }

    /**
     * @return the createTimeStart
     */
    public Date getCreateTimeStart() {
        return this.createTimeStart;
    }

    /**
     * @return the createTimeEnd
     */
    public Date getCreateTimeEnd() {
        return this.createTimeEnd;
    }

    /**
     * @return the orderTypes
     */
    public List<OrderType> getOrderTypes() {
        return orderTypes;
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param orderCode the orderCode to set
     */
    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param paymentStatus the paymentStatus to set
     */
    public void setPaymentStatus(String paymentStatus) {
        this.paymentStatus = paymentStatus;
    }

    /**
     * @param createTimeStart the createTimeStart to set
     */
    public void setCreateTimeStart(Date createTimeStart) {
        this.createTimeStart = createTimeStart;
    }

    /**
     * @param createTimeEnd the createTimeEnd to set
     */
    public void setCreateTimeEnd(Date createTimeEnd) {
        this.createTimeEnd = createTimeEnd;
    }

    /**
     * @param orderTypes the orderTypes to set
     */
    public void setOrderTypes(List<OrderType> orderTypes) {
        this.orderTypes = orderTypes;
    }
    
    public void setOrderTypes(String orderTypes) {
        this.orderTypes = null;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }
}
